/**
 * 搜索结果
 * @author
 * @date 2012-11-20
 */

var colNames_user = ['','照片','姓名','专长','性别','简历','粉丝数','关注数','id'];
var colNames_exam = ['','发布人','发布人ID','标题','介绍','创建时间','项目提交数','id'];
var colNames_question = ['','提问者','提问者ID','提问者头像','标题','内容','创建时间','阅读数','回答数','id'];
var colNames;
/**
 * colModel 显示列的属性
 *
 * @type {Object}
 */
var colModelType;
var colModel_user = [
	{ name:'first', index:'first', width:"100%", align:"left", title:false },
	{ name:'portrait',index:'portrait', hidden:true }, 	
	{ name:'nickname',index:'nickname', hidden:true },  
	{ name:'major',index:'major', hidden:true }, 
	{ name:'stringsex',index:'stringsex', hidden:true },
	{ name:'experience',index:'experience', hidden:true },
	{ name:'careCount',index:'careCount', hidden:true },
	{ name:'fansCount',index:'fansCount', hidden:true },
	{ name:'id',index:'id',hidden:true}
];

var colModel_exam = [
    { name:'first', index:'first', width:"100%", align:"left", title:false },
    { name:'userName',index:'userName', hidden:true },  
    { name:'userId',index:'userId', hidden:true },  
    { name:'title',index:'title', hidden:true }, 
    { name:'introduction',index:'introduction', hidden:true },
    { name:'formatCreatetime',index:'formatCreatetime', hidden:true },
    { name:'submitNo',index:'submitNo', hidden:true },
    { name:'id',index:'id',hidden:true}
];

var colModel_question = [
    { name:'first', index:'first', width:"100%", align:"left", title:false },
    { name:'askerName',index:'askerName', hidden:true },  
    { name:'askerId',index:'askerId', hidden:true },  
    { name:'askerPortrait',index:'askerPortrait', hidden:true },
    { name:'title',index:'title', hidden:true }, 
    { name:'content',index:'introduction', hidden:true },
    { name:'formatPublishTime',index:'formatPublishTime', hidden:true },
    { name:'readCount',index:'readCount', hidden:true },
    { name:'replyCount',index:'replyCount', hidden:true },
    { name:'id',index:'id',hidden:true}
];

/**
 * rowNum 想要显示的数据行数
 *
 * @type {Number}
 */

var rowNum = 10;

/**
 * total_last 用于判定表格的总页码是否发生了改变
 *
 * @type {Number}
 */

var total_last = 0;

/**
 * loadDataSuccess 判定是否成功加载完数据
 *
 * @type {Boolean}
 */

var loadDataSuccess = false;

var keyword="";
var type="";


$(document).ready(function(){
	var url = window.location.href;
	if(url.indexOf("?")==-1){
		return;
	}
	
	var parastr = url.split("?")[1];
	var paras = parastr.split("&");
	
	if(paras[0].indexOf("keyword")!=-1){
		keyword = ""+paras[0].split("=")[1];
		type = ""+paras[1].split("=")[1];
	}
	else if(paras[1].indexOf("keyword")!=-1){
		type = ""+paras[0].split("=")[1];
		keyword = ""+paras[1].split("=")[1];
	}
	
	$("#keyword").val(decodeURI(keyword));
	
	if(type=="user"){
		colModelType = colModel_user;
		colNames = colNames_user;
		$("#type").val(type);
	}
	else if(type=="exam"){
		colModelType = colModel_exam;
		colNames = colNames_exam;
		$("#type").val(type);
	}
	else if(type=="question"){
		colModelType = colModel_question;
		colNames = colNames_question;
		$("#type").val(type);
	}
	else{
		alert("您输入的链接有误");
		return;
	}
	
	
	$( "#mySearch" ).jqGrid({
		url:encodeURI('../search/index.html?keyword='+keyword+"&type="+type),
		datatype:"json",
		mtype: "POST",
		height: "auto",
		autowidth: true,
		colNames: colNames, 
		colModel: colModelType,
		rowNum: rowNum, 
		viewrecords: true,
		emptyrecords : '暂无相关的数据,换换关键词吧',
		pager: 'mySearchPager', 
		jsonReader: {
			root: "result",
			page: "page",
			total: "total",
			records: "records",
			repeatitems: false,
			cell: "cell",
			id: "id"
		},
		
		gridComplete:function() {
			// 取消外边框
			$( "#gbox_myNews" ).css( "border", "none" );
			$( "#gbox_mySearch" ).css( "border", "none" );
			// 隐藏表头
			$( "#gview_myNews .ui-jqgrid-hdiv" ).css( "display", "none" );
			$( "#gview_mySearch .ui-jqgrid-hdiv" ).css( "display", "none" );
			// 取消列间的分界限
			$( "#gview_myNews .ui-jqgrid-bdiv td" ).css( "border-right-style", "none" );
			$( "#gview_mySearch.ui-jqgrid-bdiv td" ).css( "border-right-style", "none" );
			// 改变底边框样式
			$( "#gview_myNews .ui-jqgrid-bdiv tr:not(:first-child) td" ).css( "border-bottom", "1px solid #E5E5E5" );
			$( "#gview_mySearch .ui-jqgrid-bdiv tr:not(:first-child) td" ).css( "border-bottom", "1px solid #E6E6E6" );
			// 取消最后一行的底边框
			$( "#gview_myNews .ui-jqgrid-bdiv tr:last-child td" ).css( "border-bottom-style", "none" );
			$( "#gview_mySearch .ui-jqgrid-bdiv tr:last-child td" ).css( "border-bottom-style", "none" );
			// 设置表格行高
			//$( "#gview_myNews .ui-jqgrid-bdiv tr:gt(0)" ).css( "height", "110px" );

			/**
			 * IDs 获取当前页所有数据的id
			 *
			 * @type {Object}
			 */

			var IDs = $( "#mySearch" ).jqGrid( "getDataIDs" );
			//没有数据
			if(IDs.length==0){
				$("#mySearchPager_center").hide();
				$(".ui-paging-info").css("text-align","left");
				$(".ui-paging-info").css("font-size","15px");
				$(".ui-jqgrid-view").css("height","60px");
			}
			
			for (var i=0; i < IDs.length; i++) {
				/**
				 * everyRowData 根据某一行数据的id获取该数据所有信息
				 *
				 * @type {Object}
				 */

				var everyRowData = $( "#mySearch" ).jqGrid( "getRowData", IDs[i] );
				var metadata;
				
				
				if(type=="user"){
					// 用户信息自定义模块
					metadata ='<div class="center-mo">' +
										'<div class="center-photo">' +
											'<img width="50px" height="50px" src="../'+ everyRowData.portrait+ '" title="'+everyRowData.nickname+'的头像"/>' +
										'</div>' +
										'<div class="center-context">' +
											'<div class="main-tit">' + 
												'<strong> <a href="../user/other.html?otherId='+everyRowData.id+'" target="_blank" title="查看'+everyRowData.nickname+'的主页">' + everyRowData.nickname + '</a>：</strong>' +
												'<span>' + (everyRowData.major=="无"?"普通用户":everyRowData.major) +'  &nbsp;&nbsp;&nbsp;&nbsp;'+everyRowData.stringsex+'</span>' +
											'</div>' +
											'<div class="main-bg"><span>'+(everyRowData.experience==""?"该用户很懒,还没有相关个人介绍":everyRowData.experience)+'</span>'+
											'</div>'+
											'<div class="main-time">关注-'+everyRowData.careCount+'&nbsp;&nbsp;粉丝-'+everyRowData.fansCount+'</div>' +
										'</div>' +
										//'<div class="center-operat"><img src="../images/home/tit_icon.png"/></div>' +						
								'</div>';
				}
				else if(type=="exam"){
					// 项目信息自定义模块
					metadata ='<div class="center-mo">' +
										'<div class="center-photo">' +
											'<img width="50px" height="50px" src="../images/exam.jpg"/>' +
										'</div>' +
										'<div class="center-context">' +
											'<div class="main-tit">' + 
												'<strong><a href="../user/other.html?otherId='+everyRowData.userId+'" target="_blank" title="查看 '+everyRowData.askerName+' 主页">' + everyRowData.userName + '</a>：</strong>' +
												'<span><a href="../ppt/join.html?id='+everyRowData.id+'" target="_blank" title="项目名称 点击参与">' + everyRowData.title + '</a></span>' +
											'</div>' +
											'<div class="main-bg" title="'+everyRowData.introduction.trim()+' 简介">' +
											'<span>' + everyRowData.introduction.trim().cut(50) +'</span>' +
											'</div>' +	
											'<div class="main-time"> 当前提交数: <font color="purple" size="3px">'+everyRowData.submitNo+'</font>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' +everyRowData.formatCreatetime + '&nbsp;&nbsp;</div>' +
										'</div>' +
								'</div>';
				}
				else if(type=="question"){
					metadata ='<div class="center-mo">' +
						'<div class="center-photo">' +
							'<img width="50px" height="50px" src="../'+everyRowData.askerPortrait+'"/>' +
						'</div>' +
						'<div class="center-context">' +
							'<div class="main-tit">' + 
								'<strong><a href="../user/other.html?otherId='+everyRowData.askerId+'" target="_blank" title="查看 '+everyRowData.askerName+' 主页">' + everyRowData.askerName + '</a>：</strong>' +
								'<span><a href="../help/searchDetail.html?id='+everyRowData.id+'"target="_blank" title="点击查看详情">' + everyRowData.title + '</a></span>' +
							'</div>' +
							'<div class="main-bg" title="'+wipeoff(everyRowData.content.trim())+'" >' +
								'<span>' + wipeoff(everyRowData.content.trim()).cut(20) +'</span>' +
							'</div>' +	
							'<div class="main-time"> 当前阅读数: <font color="purple" size="3px">'+everyRowData.readCount+'</font>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'
								+'当前回答数: <font color="purple" size="3px">' +everyRowData.replyCount+'</font>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'
								+everyRowData.formatPublishTime + '&nbsp;&nbsp;</div>' +
						'</div>' +
					'</div>';
				}
									
				$( "#mySearch" ).jqGrid( 'setRowData', IDs[i], { first: metadata } );
				// 取消提示信息
				$( ".metadata" ).parent("td").attr("title","");
		
	        }		
		},
		onSelectRow: function(rowid) {}

	});


});


/**
 * 显示内容title时，如果内容里带有“<img />”等<br>
 * 会造成格式错误，所以去除包括“<>"之间的所有内容
 * @param str
 */
function wipeoff(str){
	if(str==undefined || str.trim()==""){
		return "";
	}
	else{
		return str.replace(new RegExp("<.*>","gm"),"");
	}
};
